// components/t-map/t-map.js
import {
  request,
  navigationHandler
} from '../../utils/requestUtils';
Component({
  options: {
    multipleSlots: true,
    styleIsolation: 'shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    latitude: {
      type: String,
      value: '30.658404'
    },
    longitude: {
      type: String,
      value: '104.065856'
    },
    //1 收运端；2.业务端
    type: {
      type: String,
      value: '1'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    query: {
      equipmentStatus: true, //待安装：true.是，fasle:否
      rtStatus: false, //待更换：true.是，fasle 否
      switchStatus: false, //是否更换完成 true.是： fasle:'否',
      merchantName: '',
      busAllMerchant:true,//业务员端，全部商家,
      busAbort:false,//是否撤机
    },
    subKey: 'X6NBZ-JREKL-TCWP2-EK23W-R6QR2-DJBAQ',
    enable3d: false,
    showLocation: true,
    showCompass: false,
    enableOverlooking: false,
    enableZoom: true,
    enableScroll: true,
    enableRotate: false,
    drawPolygon: false,
    enableSatellite: false,
    enableTraffic: false,
    // latitude: '30.658404',
    // longitude: '104.065856',
    markers: [],
    circles: [],
    polylines: [],
    polygons: [],
    currentMarker: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //点击标记点
    handleMarkerTap(e) {
      if(this.properties.type == 2)return
      const marker = this.data.markers.find(item => item.id == e.detail.markerId);
      this.setData({
        currentMarker: marker
      });
      console.log('currentMarker', this.data.currentMarker)
    },
    //导航
    async navigation() {
      await navigationHandler(this.data.currentMarker.latitude, this.data.currentMarker.longitude,this.data.currentMarker.merchantName)
    },
    //跳转到商家详情
    toDetals() {
      wx.navigateTo({
        url: '/pages/worker_merchant_details/index?merchantId=' + this.data.currentMarker.merchantId,
      })
    },
    //点击地图
    mapTap(e) {
      //   let detail = e.detail;
      //   console.log('点击地图',detail)
      //   let markers  = new Array({
      //     id: Math.random(),
      //     latitude: detail.latitude,
      //     longitude: detail.longitude,
      //     width: 30,
      //     height: 30,
      //   });
      //   this.setData({
      //     markers: markers
      //   });
    },

    //点击待安装按钮
    equipmentStatusChange(event) {
      this.setData({
        'query.equipmentStatus': event.detail,
      });
      this.workerMap()
    },
    //勾选待更换按钮
    rtStatusChange(event) {
      this.setData({
        'query.rtStatus': event.detail,
      });
      this.workerMap()
    },
    //更换完成勾选
    switchStatusChange(event) {
      this.setData({
        'query.switchStatus': event.detail,
      });
      this.workerMap()
    },
    //勾选全部商家
    busAllMerchantChange(event){
      this.setData({
        'query.busAllMerchant': event.detail,
      });
      this.workerMap()
    },
    //是否撤机勾选
    busAbortChange(event){
      this.setData({
        'query.busAbort': event.detail,
      });
      this.workerMap()
    },
    //点击搜索按钮
    search(query) {
      console.log('query', query)
      this.setData({
        'query.merchantName': query.keywords?query.keywords:'',
      })
      this.workerMap()

    },
    //师傅端查询统计信息
    async workerMap() {
      const result = await request({
        url: `/api/bus/statistics/worker/map/${this.properties.type}`,
        method: "GET",
        data: this.data.query
      })
      if(result.data.data && result.data.data.length>0){
        result.data.data.forEach((v,index)=>{
          console.log('index',index)
          v.id = index;
          v.label={
            content:v.merchantName
          }
        })
      }
      this.setData({
        markers: result.data.data
      })
      console.log('markers', this.data.markers)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  // attached: function () {
  //   this.workerMap()
  //   this.setData({
  //     markers: [
  //       {
  //         id: 1,
  //         latitude: 30.555705,
  //         longitude: 104.010372,
  //         merchantAddr: '温江区柳城街办春江南路398号春江花源2栋1层20，21室',
  //         properties: {
  //           name: '川味小厨'
  //         },
  //         width: 24,
  //         height: 30,
  //         merchantAddr: '温江区柳城街办春江南路398号春江花源2栋1层20，21室',
  //         iconPath: 'https://csy-test1.oss-cn-chengdu.aliyuncs.com/2024/04/25/dd19bf42df5147098e3454ccaf4845d8.png',
  //         label: {
  //           content: '川味小厨'
  //         }
  //       },
  //       {
  //         id: 2,
  //         latitude: 30.557546,
  //         longitude: 103.99915,
  //         properties: {
  //           name: '川味小厨22'
  //         },
  //         width: 24,
  //         height: 30,
  //         iconPath: 'https://csy-test1.oss-cn-chengdu.aliyuncs.com/2024/04/25/8922157f9e7b4877ba80928864e8c67b.png',
  //         label: {
  //           content: '川味小厨22'
  //         }
  //       }

  //     ]
  //   });
  // },


})
